#include <lpc23xx.h>
#include <RTL.h>
#include "LCD.h"

OS_TID  id1,id2,id3;
typedef struct{
unsigned int ADresult;
}MESSAGE;
os_mbx_declare (MsgBox,16);
_declare_box (mpool,sizeof(MESSAGE),16);



__task void task1(void);
__task void task2(void);

int main(void)
{
lcd_init();					   
lcd_clear();				  
set_cursor(0,0);			   
lcd_print("Mailbox  Example");
FIO2DIR=0x000000FF;
PCONP |= 0x00001000;  
PINSEL1 |= 0x00004000;		
AD0CR = 0x00200D00;
AD0CR |= 0x01000000;
_init_box (mpool, sizeof(mpool),sizeof(MESSAGE));
os_sys_init_prio (task1,0x01);
	 
}
__task void task1(void){
int result;
   MESSAGE *mptr;
   id1 = os_tsk_self ();
   id2 = os_tsk_create (task2,0x01);
   os_mbx_init (MsgBox, sizeof(MsgBox));
	while(1)
	{
	mptr = _alloc_box (mpool);
	if((AD0GDR &0x80000000))
        {
            result = ((AD0GDR >>8)&0xFF);	  //Agaf
            mptr->ADresult = result;
            AD0CR |= 0x01000000;
        }
  
    	
	os_mbx_send (MsgBox, mptr, 0xffff);
	os_evt_set (0x0004, id2);
	os_evt_wait_or (0x0004, 0xffff);
	os_dly_wait (0);
	}

}
__task void task2(void){
	MESSAGE* rptr;
	while(1)
	{
	os_evt_wait_or (0x0004, 0xffff);
	os_mbx_wait (MsgBox, (void **)&rptr, 0xffff);
	
	FIO2CLR=0x000000ff;
	FIO2SET=rptr->ADresult;
	_free_box (mpool, rptr);
	os_dly_wait (0);
	os_evt_set (0x0004, id1);	
	//os_dly_wait(50);

	}

}
